package org.havenapp.main;

import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.util.Log;

import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTNotificationMessage;
import com.igexin.sdk.message.GTTransmitMessage;


/**
 * 继承 GTIntentService 接收来自个推的消息, 所有消息在线程中回调, 如果注册了该服务, 则务必要在 AndroidManifest中声明, 否则无法接受消息<br>
 * onReceiveMessageData 处理透传消息<br>
 * onReceiveClientId 接收 cid <br>
 * onReceiveOnlineState cid 离线上线通知 <br>
 * onReceiveCommandResult 各种事件处理回执 <br>
 */
public class DemoIntentService extends GTIntentService {

    public DemoIntentService() {

    }

    @Override
    public void onReceiveServicePid(Context context, int pid) {
    }

    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage msg) {
        Log.e(TAG, "onReceiveMessageData -> " + "msg.getPayload = " + msg.getPayload());
        Log.e(TAG, "onReceiveMessageData -> " + "msg.getMessageId = " + msg.getMessageId());
        Log.e(TAG, "onReceiveMessageData -> " + "msg.getPayloadId = " + msg.getPayloadId());
        Log.e(TAG, "onReceiveMessageData -> " + "msg.getTaskId = " + msg.getTaskId());

        String s = changeString(msg.getPayload());
        Log.e(TAG, "changeString -> " + s);
        alrt();
//        {time=Fri Apr 13 15:38:48 GMT+08:00 2018, type=监听到了异响}

//        time,Fri Apr 13 15:38:48 GMT+08:00 2018, type,监听到了异响


        // Log.e("buxq", "onReceiveMessageData: " + pushBean.toString());

    }

    Ringtone ringtone;

    private void alrt() {
        JCMediaManager.instance().prepare();
//        try {
//            if (ringtone == null) {
//                Uri uri = RingtoneManager
//                        .getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
//                ringtone = RingtoneManager.getRingtone(getApplicationContext(), uri);
//                Class<Ringtone> clazz = Ringtone.class;
//                Field field = clazz.getDeclaredField("mLocalPlayer");
//                field.setAccessible(true);
//                MediaPlayer target = (MediaPlayer) field.get(ringtone);
//                target.setLooping(true);
//                ringtone.setStreamType(AudioManager.STREAM_RING);
//                Log.e("buxq", "alert path: " + uri.toString());
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        ringtone.play();


        Intent musicIntent = new Intent();
        musicIntent.setAction("action.changesong");
        this.sendBroadcast(musicIntent);

    }


    public String changeString(byte[] payload) {
        return new String(payload);
    }


    @Override
    public void onReceiveClientId(Context context, String clientid) {
        Log.e(TAG, "onReceiveClientId -> " + "clientid = " + clientid);
        MonitorActivity.cid = clientid;

    }

    @Override
    public void onReceiveOnlineState(Context context, boolean online) {
    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage cmdMessage) {
    }

    @Override
    public void onNotificationMessageArrived(Context context, GTNotificationMessage gtNotificationMessage) {

    }

    @Override
    public void onNotificationMessageClicked(Context context, GTNotificationMessage msg) {
    }
}